package com.qjd.girl.service;

import com.qjd.girl.domain.Girl;
import com.qjd.girl.enums.ResultEnum;
import com.qjd.girl.expection.GirlExpection;
import com.qjd.girl.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by qiangjindong
 * 2018-08-03 14:22
 */
@Service
public class GirlService {

    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void inertTwo() {
        Girl girlA = new Girl();
        girlA.setAge(16);
        girlA.setCupSize("AAA");
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setAge(17);
        girlB.setCupSize("B");
        girlRepository.save(girlB);
    }

    /**
     * 通过id获取女生的年龄
     * @param id
     * @throws GirlExpection
     */
    public void getAge(Integer id) throws GirlExpection {
        Integer age = girlRepository.findById(id).get().getAge();
        if (age < 10) {
            throw new GirlExpection(ResultEnum.PRIMARY_SCHOOL);
        } else if (age < 16) {
            throw new GirlExpection(ResultEnum.MIDDLE_SCHOOL);
        }
    }

    /**
     * 通过id获取一个女生信息
     * @param id
     * @return
     */
    public Girl findOne(Integer id) {
        return girlRepository.findById(id).get();
    }
}
